package waggle.client.forms;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;

import waggle.client.i18n.AppConstants;
import waggle.client.ApplicationView;
import waggle.client.DataSwitch;
import waggle.client.bean.User;
import waggle.client.bean.Email;

public class ForgotPasswordForm extends Form {

	  /**
	   * The application constants.
	   * 
	   */
	  private final static AppConstants constants = ApplicationView.getConstants();

	  /**
	   * The email form input.
	   * 
	   */
	  private final FormInput email = new EmailFormInput(constants.Email(), false);

	  public ForgotPasswordForm(String name) {
	    this(name, null);
	  }

	  public ForgotPasswordForm(String name, FormListener listener) {
	    super(name, true, Layout.VERTICAL, listener);
	    addInput(email);
	  }

	  @Override
	  protected void onCancel() {
	    History.newItem("");
	  }

	  @Override
	  protected void onSubmit() {
	    Email mail = createEmail();
	    DataSwitch.get().resetUserPassword(email.getInputValue(), mail,
	            new AsyncCallback() {
	              public void onFailure(Throwable caught) {
	                fireOnSubmitFailed(caught);
	              }

	              public void onSuccess(Object result) {
	                fireOnSubmitSuccess(result);
	              }
	            });
	  }

	  private Email createEmail() {
	    Email email = new Email();
	    email.setSubject(constants.PasswordRecoverySubject());
	    email.setContent(constants.RecoveryNotificationMsg());
	    email.initCommonFields();
	    return email;
	  }

	}
